﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Chinesys.Domain.Crawling
{
    public class LinkHelper
    {       
    //    private const string _LINK_REGEX = "href=\"[a-zA-Z./:&\\d_-]+\"";
        
    //    public List<string> GoodUrls { get; set; }
    //    public List<string> BadUrls { get; set; }
    //    public List<string> OtherUrls { get; set; }
    //    public List<string> ExternalUrls { get; set; }
    //    public List<string> Exceptions { get; set; }


    //    /// <summary>
    //    /// Parses a page looking for links.
    //    /// </summary>
    //    /// <param name="page">The page whose text is to be parsed.</param>
    //    /// <param name="sourceUrl">The source url of the page.</param>
    //    public void ParseLinks(Page page, string sourceUrl)
    //    {
    //        MatchCollection matches = Regex.Matches(page.Text, _LINK_REGEX);

    //        for (int i = 0; i <= matches.Count - 1; i++)
    //        {
    //            Match anchorMatch = matches[i];

    //            if (anchorMatch.Value == String.Empty)
    //            {
    //                BadUrls.Add("Blank url value on page " + sourceUrl);
    //                continue;
    //            }

    //            string foundHref = null;
    //            try
    //            {
    //                foundHref = anchorMatch.Value.Replace("href=\"", "");
    //                foundHref = foundHref.Substring(0, foundHref.IndexOf("\""));
    //            }
    //            catch (Exception exc)
    //            {
    //                Exceptions.Add("Error parsing matched href: " + exc.Message);
    //            }


    //            if (!GoodUrls.Contains(foundHref))
    //            {
    //                if (!IsAWebPage(foundHref))
    //                {
    //                    foundHref = Crawler.FixPath(sourceUrl, foundHref);
    //                    OtherUrls.Add(foundHref);
    //                }
    //                else
    //                {
    //                    GoodUrls.Add(foundHref);
    //                }

    //            }
    //        }
    //    }
        
    //    /// <summary>
    //    /// Is the url to an external site?
    //    /// </summary>
    //    /// <param name="url">The url whose externality of destination is in question.</param>
    //    /// <returns>Boolean indicating whether or not the url is to an external destination.</returns>
    //    private static bool IsExternalUrl(string url)
    //    {
    //        if (url.IndexOf(ConfigurationManager.AppSettings["authority"]) > -1)
    //        {
    //            return false;
    //        }
    //        else if (url.Substring(0, 7) == "http://" || url.Substring(0, 3) == "www" || url.Substring(0, 7) == "https://")
    //        {
    //            return true;
    //        }

    //        return false;
    //    }

    //    /// <summary>
    //    /// Is the value of the href pointing to a web page?
    //    /// </summary>
    //    /// <param name="foundHref">The value of the href that needs to be interogated.</param>
    //    /// <returns>Boolen </returns>
    //    private static bool IsAWebPage(string foundHref)
    //    {
    //        if (foundHref.IndexOf("javascript:") == 0)
    //            return false;

    //        string extension = foundHref.Substring(foundHref.LastIndexOf(".") + 1, foundHref.Length - foundHref.LastIndexOf(".") - 1);
    //        switch (extension)
    //        {
    //            case "jpg":
    //            case "css":
    //                return false;
    //            default:
    //                return true;
    //        }

    //    }
    }
}
